package com.th.supcom.pds.impl.service.adapter;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.pojo.InpPhamApplyMaster;
import com.th.supcom.pds.domain.pojo.InpPhamApplyPatInfo;
import com.th.supcom.pds.domain.vo.InpPhamApplyDetailVO;
import com.th.supcom.pds.domain.vo.InpPhamDispenseDetailVO;
import com.th.supcom.pds.impl.service.internal.phamapply.PhamApplyConvertUtils;
import com.th.supcom.pds.impl.service.internal.phamapply.PhamDispenseConvertUtils;
import com.th.supcom.pds.internal.api.phamapply.OrderBasicService;
import com.th.supcom.pds.medicare.api.AdapterOrdersService;

public class AdapterOrdersServiceImpl implements AdapterOrdersService
{
    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (AdapterOrdersServiceImpl.class);
    private OrderBasicService m_orderBasicService;

    public void setOrderBasicService (OrderBasicService orderBasicService)
    {

        m_orderBasicService = orderBasicService;
    }

    @Override
    public List <InpPhamApplyDetailVO> obtainOrdersScheduleItems (List <InpPhamApplyPatInfo> patInfos, InpPhamApplyMaster master)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List <InpPhamApplyDetailVO> findOrderExecListByExample (InpPhamApplyDetailVO inpPhamApplyDetailVO)
    {
        return m_orderBasicService.findOrderExecListByExample (inpPhamApplyDetailVO);
    }

    @Override
    public List <InpPhamApplyDetailVO> findOrderContentByPaivisitAndOrderNo (List <InpPhamApplyDetailVO> detailVOList)
    {
        List <InpPhamApplyDetailVO> orderList;
        orderList = m_orderBasicService.findOrderContentByPaivisitAndOrderNo (detailVOList);
        Map <Long, List <InpPhamApplyDetailVO>> orderMap = PhamApplyConvertUtils.convertListToMapByPaivisitID (orderList);
        Map <Long, List <InpPhamApplyDetailVO>> applyDetailMap = PhamApplyConvertUtils.convertListToMapByPaivisitID (detailVOList);
        Set <Map.Entry <Long, List <InpPhamApplyDetailVO>>> set = applyDetailMap.entrySet ();
        for (Iterator <Map.Entry <Long, List <InpPhamApplyDetailVO>>> it = set.iterator (); it.hasNext ();)
        {
            Entry <Long, List <InpPhamApplyDetailVO>> entry = (Map.Entry <Long, List <InpPhamApplyDetailVO>>) it.next ();
            List <InpPhamApplyDetailVO> detailTemp = entry.getValue ();
            List <InpPhamApplyDetailVO> orderTemp = orderMap.get (entry.getKey ());
            if (null == orderTemp)
            {
                continue;
            }
            for (InpPhamApplyDetailVO detailVO : detailTemp)
            {
                for (InpPhamApplyDetailVO orderVO : orderTemp)
                {
                    if (!detailVO.getPaiVisitId ().equals (orderVO.getPaiVisitId ()))
                    {
                        continue;
                    }
                    if (!detailVO.getOrderNo ().equals (orderVO.getOrderNo ()))
                    {
                        continue;
                    }
                    if (!detailVO.getOrderSubNo ().equals (orderVO.getOrderSubNo ()))
                    {
                        continue;
                    }
                    detailVO.setOrderContent (orderVO.getOrderContent ());
                    detailVO.setRepetition (orderVO.getRepetition ());
                    detailVO.setDosage (orderVO.getDosage ());
                    detailVO.setPhamUnit (orderVO.getPhamUnit ());
                }
            }
        }

        return detailVOList;
    }

    @Override
    public List <InpPhamDispenseDetailVO> findDispensePhamOrderContent (List <InpPhamDispenseDetailVO> list)
    {
        List <InpPhamDispenseDetailVO> orderList;
        orderList = m_orderBasicService.findDispensePhamOrderContent (list);
        Map <Long, List <InpPhamDispenseDetailVO>> orderMap = PhamDispenseConvertUtils.convertListToMapByPaivisitID (orderList);
        Map <Long, List <InpPhamDispenseDetailVO>> dispenseDetailMap = PhamDispenseConvertUtils.convertListToMapByPaivisitID (list);
        Set <Map.Entry <Long, List <InpPhamDispenseDetailVO>>> set = dispenseDetailMap.entrySet ();
        for (Iterator <Map.Entry <Long, List <InpPhamDispenseDetailVO>>> it = set.iterator (); it.hasNext ();)
        {
            Entry <Long, List <InpPhamDispenseDetailVO>> entry = (Map.Entry <Long, List <InpPhamDispenseDetailVO>>) it.next ();
            List <InpPhamDispenseDetailVO> detailTemp = entry.getValue ();
            List <InpPhamDispenseDetailVO> orderTemp = orderMap.get (entry.getKey ());
            for (InpPhamDispenseDetailVO detailVO : detailTemp)
            {
                for (InpPhamDispenseDetailVO orderVO : orderTemp)
                {
                    if (detailVO.getPaiVisitId () != orderVO.getPaiVisitId ())
                    {
                        continue;
                    }
                    if (!detailVO.getOrderNo ().equals (orderVO.getOrderNo ()))
                    {
                        continue;
                    }
                    if (!detailVO.getOrderSubNo ().equals (orderVO.getOrderSubNo ()))
                    {
                        continue;
                    }
                    detailVO.setOrderContent (orderVO.getOrderContent ());
                    detailVO.setRepetition (orderVO.getRepetition ());
                    detailVO.setDosage (orderVO.getDosage ());
                    detailVO.setPhamUnit (orderVO.getPhamUnit ());
                }
            }
        }

        return list;
    }

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, XuNuo Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

}
